<template>
  <div class="bottom-menu">
    <label for="checkAll">
      <check-button class="select-all" @backColorClick="backColorClick" v-model="totalPrice" :checked="[Object.keys($store.state.cartList).length !== 0 ? $store.state.checked : false,0]"></check-button>
      <span id="checkAll">全选</span>
    </label>
    <span class="total-price">合计: ¥{{totalPrice}}</span>
    <span class="buy-product">去结算({{getLength}})</span>
  </div>
</template>

<script>
  import CheckButton from "views/cart/children/CheckButton";
  import {isSelectedMixin} from "common/mixin";

  export default {
    name: "CartBottomBar",
    components: {
      CheckButton
    },
    mixins: [isSelectedMixin],
    computed: {
      /**
       * 计算长度
       */
      getLength(){
        let cartList = this.$store.state.cartList;
        return cartList.filter((res) => {
          return res.checked === true;
        }).length;
      },
      /**
       * 计算选中的价格
       * @returns {string}
       */
      totalPrice(){
        let cartList = this.$store.state.cartList;

        return cartList.filter((item) => item.checked === true).reduce((pre,cur) => pre + (parseFloat(cur.price) * cur.cartCount),0).toFixed(2);
      }
    },
    methods: {
      backColorClick(){
        let isSelected = this.isSelected;//先判断是否有未有选中的

        this.$store.state.cartList.forEach((item,index) => {
          if (isSelected) {
            item.checked = true;
            this.$store.commit('setChecked',true);
          }else {
            item.checked = false;
            this.$store.commit('setChecked',false);
          }
        });
      }
    }
  }
</script>

<style scoped>
  .bottom-menu {
    width: 100%;
    height: 44px;
    background-color: #eee;
    position: fixed;
    bottom: 49px;
    left: 0;
    box-shadow: 0 -2px 3px rgba(0, 0, 0, .2);
    font-size: 14px;
    color: #888;
    line-height: 44px;
    padding-left: 35px;
    box-sizing: border-box;
  }

  .bottom-menu .select-all {
    position: absolute;
    line-height: 0;
    left: 12px;
    top: 13px;
  }

  .bottom-menu .total-price {
    margin-left: 15px;
    font-size: 16px;
    color: #666;
  }

  .bottom-menu .buy-product {
    background-color: orangered;
    color: #fff;
    width: 100px;
    height: 44px;
    text-align: center;
    line-height: 44px;
    float: right;
  }
</style>